package Boke;

import java.util.Scanner;

public class See {
    //用两个线程实现出内存可见性线程安全问题
     static int cout=0;
    public static void main(String[] args) throws InterruptedException {
        // int a=0;

        //线程t1负责对变量的读和判断
        Thread t1=new Thread(()->{
            System.out.println("cout==0,t1开始");
            //此处编译器已对重复读取的指令进行优化(只读第一次)
            while(cout==0){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //让编译器产生优化
            }
            System.out.println("cout!=0,t1结束");
        });

        //线程t2负责对t1读取判断的变量进行修改，控制线程t1退出
        Thread t2=new Thread(()->{
            System.out.println("t2开始");
            Scanner scanner=new Scanner(System.in);
            System.out.println("请输入一个非零值结束线程t1");
            cout=scanner.nextInt();
            System.out.println("t2结束");
        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();
    }

}
